/*
    doc.me -- Ejscript documentation build file
 */

Me.load({

    targets: {
        api: {
            /* Disabled because the API is internal only */
            enable: false,
            action: `
                trace('Generate', 'Script library documentation')
                let bin = Cmd.locate('ejsmod').dirname
                let src = me.dir.src.join('src')
                let docmod = Path('').temp().replaceExt('mod')
                let files = Path('../src').files('*.es')
                files.push('../paks/ejs.version/Version.es')
                run(bin.join('ejsc') + ' --doc --bind --out ' + docmod + ' ' + files.join(' '))
                let edir = Path('dist/ref/api')
                rmdir([edir.join('*.html'), edir.join('*.css'), edir.join('images/*')])
                run(bin.join('ejsmod') + ' --html ' + edir + ' ' + docmod)
                cp('dist/ref/api/masters/*.html', edir)
                cp('dist/ref/api/masters/*.jpg', edir.join('images'))
                docmod.remove()
            `,
        },

        validate: {
            depends: [ 'gen-doc' ],
            action: `
                for each (f in Path('dist').files('**/*.html')) {
                    let data = f.readString()
                    data = data.replace('700|Open', '').
                        replace(/me.download.html/g, 'download/me.html').
                        replace(/developers.contributors.html/g, 'scripts/contributors.esp').
                        replace(/nowrap="nowrap"/g, '').
                        replace(/https:..embedthis.com.makeme.licensing.html/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.makeme.doc.contents.building.htm/g, 'https://embedthis.com/').
                        replace(/https:..embedthis.com.developers./g, 'https://embedthis.com/').

                        replace(/nowrap/g, '')
                    trace('Patch', f)
                    f.write(data)
                }
                for each (f in Path('dist/man').files('*.html')) {
                    f.write(f.readString().toLowerCase())
                }
                let path = Path('dist/ref/api/index.html')
                path.write('<!DOCTYPE html><html><head><title>dummy</title></head><body></body></html>\n')
                trace('Listen', 'expansive --nowatch')
                Cmd.run('expansive --nowatch')
            `
        },
    },
})
